package com.ufaud.ais.sys.webapp.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;

import com.ufaud.ais.sys.entity.MenuItem;
import com.ufaud.ais.sys.service.IMenuItemService;
import com.ufaud.ais.sys.view.EmployeeView;

/**
 * 
 * @author Chenjd 2010-6-24
 * 
 */
public class HomeAction {
	private static final Log log = LogFactory.getLog(HomeAction.class);

	private static final String CUR_USER = "curUser";

	private EmployeeView employee;
	List<MenuItem> userMenuList;
	private IMenuItemService menuItemService;

	/**
	 * 
	 * @author Chenjd 2010-6-24
	 * @return
	 */
	public String checkIn() {
		employee = (EmployeeView) ServletActionContext.getRequest().getSession().getAttribute(CUR_USER);

		if (employee != null) {
			log.debug(employee.getAccountName() + " already login.");
			return "success";
		}

		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication == null || authentication.getPrincipal() == null) {
			return "fail";
		}
		employee = (EmployeeView) authentication.getPrincipal();

		// 依据相应的授权取得能使用的资源
		log.debug("finding resources in authentication...");

		ServletActionContext.getRequest().getSession().setAttribute(CUR_USER, employee);
		
		userMenuList= menuItemService.getMenuItemListInTreeStructureByUserId(employee.getEmployeeId());

		return "success";
	}

	public String logout() {
		return "success";
	}

	public void setMenuItemService(IMenuItemService menuItemService) {
		this.menuItemService = menuItemService;
	}

	public List<MenuItem> getUserMenuList() {
		return userMenuList;
	}

}
